﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LGSVR.Common;
using LGSVR.BLL.Terminal;

namespace LGSVR_WEB.terminal
{
    public partial class CardTypeEdit :  DataBasePage
    {
        private int Pid
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["ID"]) ? 0 : int.Parse(Request.QueryString["ID"]);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "out", "<script>alert('系统超时或服务器故障，请退出重新登录！');window.parent.parent.navigate('../Logout.aspx');</script>");
            }
            if (!IsPostBack)
            {
                PageInit();
            }
        }
        void PageInit()
        {
            Dictionary<string, string> dicSingle;

            // 编辑
            if (!string.IsNullOrEmpty(Request.QueryString["action"]) && Request.QueryString["action"].ToLower() == "edit")
            {
                btnSave.Text = "更 新";
                if (Utils.IsNumber(Request.QueryString["ID"]))
                {
                    dicSingle = GetSigle("cardType", "ID=" + Request.QueryString["ID"]);

                    if (dicSingle != null)
                    {
                        txtcontent.Text = dicSingle["content"];

                        txtTypeID.Text = dicSingle["TypeID"];
                        txttypeName.Text = dicSingle["TypeName"];
                        chkstate.Checked = Utils.ToBool(dicSingle["state"]);
                    }
                    else
                    {
                        MsgContent = ShowMessage("数据加载失败。", MessageType.Error, true, false);
                    }
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            CardTypeBLL cardTypeBll = new CardTypeBLL();
            if (cardTypeBll.IsHasTypeID(txtTypeID.Text.Trim(), Pid))
            {
                MsgContent = ShowMessage("已存在该类型名称，请更换！", MessageType.Error, false, true);
                return;
            }

            Dictionary<string, string> dicParam;

            dicParam = new Dictionary<string, string>();

            dicParam.Add("TypeID", txtTypeID.Text.Trim());
            dicParam.Add("typeName", txttypeName.Text.Trim());          

            if (chkstate.Checked)
            {
                dicParam.Add("state", "1");
            }
            else
            {
                dicParam.Add("state", "0");
            }

            dicParam.Add("content", txtcontent.Text.Trim());

            // 编辑
            if (!string.IsNullOrEmpty(Request.QueryString["action"]) && Request.QueryString["action"].ToLower() == "edit")
            {
                if (Utils.IsNumber(Request.QueryString["ID"]))
                {
                    Edit("cardType", dicParam, "ID=" + Pid);

                    if (Result > 0)
                    {
                        MsgContent = ShowMessage("恭喜您，更新成功！", MessageType.Success, false, true);
                        NoRefresh();
                    }
                    else
                    {
                        if (Result == -100)
                        {
                            MsgContent = ShowMessage("对不起，操作失败，程序参数异常。", MessageType.Error, false, true);
                        }
                        else
                        {
                            MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
                        }
                    }
                }
            }
            else  // 新增
            {
                Insert("cardType", dicParam);

                if (Result > 0)
                {
                    MsgContent = ShowMessage("恭喜您，添加成功！", MessageType.Success, false, true);
                    NoRefresh();
                }
                else
                {
                    MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
                }
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["backUrlParam"]))
            {
                Response.Redirect("CardTypeList.aspx?" + Request.QueryString["backUrlParam"], false);
            }
            else
            {
                Response.Redirect("CardTypeList.aspx", false);
            }
        }
    }
}